package com.runa.rccprotocol.analysis;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.persistence.DateUtil;
import com.runa.persistence.service.LogService;
import com.runa.persistence.util.LogUtil;
import com.runa.protocol.analysis.MetaBufferAnalysis;
import com.runa.protocol.analysis.ShellBufferAnalysis;
import com.runa.protocol.util.ByteUtil;
import com.runa.rccprotocol.dto.qiyang.QiYangConcentratorHeartBeat;
import com.runa.rccprotocol.dto.qiyang.QiYangConcentratorRegister;
import com.runa.rccprotocol.dto.qiyang.QyConcentratorResponseAnalysis;
import com.runa.rccprotocol.dto.qiyang.QyConcentratorResponseData;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
 * 2024/7/2 10:42
 * zhangYB
 */
@Slf4j
public class QiYangAnalysis extends ShellBufferAnalysis<byte[]> {


    public QiYangAnalysis(MetaBufferAnalysis analysis) {
        super(analysis, 0);
    }

    @Override
    public int fill(byte[] bytes, int length, String netEquNo) {
        byte[] data = new byte[length];
        int cnt = 0;
        for (int i=0; i<length; i++) {
            if (bytes[i] == (byte) 0xfd && bytes[i+1] == (byte) 0xed) {
                data[cnt++] = (byte) 0xfd;
                i++;
            } else if (bytes[i] == (byte) 0xfd && bytes[i+1] == (byte) 0xee) {
                data[cnt++] = (byte) 0xfe;
                i++;
            } else {
                data[cnt++] = bytes[i];
            }
        }
        length = cnt;

        log.debug("七洋: {} {}", netEquNo, ByteUtil.byteToHexString(data, 0, length));
        this.netEquNo = netEquNo;
        if (length == 1) {
            // 心跳检测
            interData = new QiYangConcentratorHeartBeat(netEquNo);
            LogUtil.linkDetect(netEquNo, "七洋", data, length, "心跳");
            return 0;
        }
        if (length < 4) {
            return 0;
        }

        for (int i = 0; i < length; i++) {
            if ((data[i] & 0xff) == 0xfe && (data[i+1] & 0xff) == 0x08 && (data[i+2] & 0xff) == 0x01) {
                return analysisControlResponse(data, length, i+1);
            }
        }

        switch(length) {
            case 21:
                this.netEquNo = ByteUtil.bcd2str(data, 0, 4, false);
                LogUtil.linkDetect(netEquNo, "七洋", data, length, "登录");
                interData = new QiYangConcentratorRegister(this.netEquNo);
                break;
            case 48:
                analysis.fill(data, length, netEquNo);
                interData = analysis.gain();
                break;
            case 34:
                analysisReadValveResponse(data, length);
                break;

            default:
                LogService.saveErrorLog("七洋未解析", "QY", data, length);
        }
        return 0;
    }

    private void analysisReadValveResponse(byte[] data, int length) {
        QyConcentratorResponseData response =  new QyConcentratorResponseData();
        response.setAddress(netEquNo);
        response.setMessage(ByteUtil.byteToHexString(data, 0, length));
        ValveVO valveVO = new ValveVO();
        valveVO.setValveSerial(ByteUtil.bcd2str(data, 0, 4, false));
        valveVO.setOpenTime((float)(data[12] + data[13] << 8  + data[14] << 16  + data[15] << 24));
        valveVO.setActValue((float)((data[20] & 0x01) == 0x01 ? 100 : 0));
        valveVO.setActOpen((float)((data[20] & 0x01) == 0x01 ? 100 : 0));
        Date date = DateUtil.stringToDate(ByteUtil.byteToHexStringFlip(data, 26, 6), "yyMMddHHmmss");
        valveVO.setValveDate(date);
        valveVO.setReadTime(date);
        response.addData(valveVO);
        interData = response;
    }

    private int analysisControlResponse(byte[] data, int length, int i) {
        for (i += 2; i < length; i++) {
            if ((data[i] & 0xff) == 0x68 && (data[i+1] & 0xff) == 0x49) {
                QyConcentratorResponseData response =  new QyConcentratorResponseData();
                response.setAddress(netEquNo);
                response.setMessage(ByteUtil.byteToHexString(data, 0, length));
                ValveVO valveVO = new ValveVO();
                valveVO.setValveSerial(ByteUtil.byteToHexStringFlip(data, i+2, 4));
                response.addData(valveVO);
                interData = response;
                return 0;
            }
        }
        return 0;
    }

    public static void main(String[] args) {
        String s = "493702150000000000000000000000000000000000000000000000000000000000000000000000000000000000000097";
        byte[] data = ByteUtil.hexStringToByte(s);
        QiYangAnalysis qiYangAnalysis = new QiYangAnalysis(new QyConcentratorResponseAnalysis());
        qiYangAnalysis.fill(data, data.length, null);
        System.out.println(JSON.toJSONString(qiYangAnalysis.gain()));
    }


}
